#include "tcp_server.h"
#include "t_thread.h"

tcp_server::tcp_server(tcp_sink* sink)
{
	func_lock s_lock;
	m_log = new logfile("tcp_server");
	m_sink = sink;

	m_thread = new tcp_thread(sink);
};

tcp_server::~tcp_server()
{
	func_lock s_lock;
	delete m_log;
	
	if(m_thread != 0)
	{
	   m_thread->StopThread();
	   delete m_thread;
	}

	m_log->WriteTrace("tcpserver is stop");
}

int tcp_server::sendbuf(uint32 socket, char* buf, uint32 buf_size)
{	
	func_lock s_lock;
	std::map<uint16, t_socket_server*>::iterator iter;
	iter = m_portmap.begin();

	return iter->second->sendbuf(socket,buf,buf_size);
}

bool tcp_server::del_socket(uint32 socket)
{
	func_lock s_lock;
	char ll[1023] = {0};
	sprintf(ll,"del socket socket=%d",socket);
	m_log->WriteTrace(ll);

	std::map<uint32,uint16>::iterator iter;
    iter = m_socketmap.find(socket);

	if(iter == m_socketmap.end())
	{
		m_log->WriteTrace("socket map error");
	    return false;
	}

	std::map<uint16, t_socket_server*>::iterator iter1;
	iter1 = m_portmap.find(iter->second);
 
	if(iter1 == m_portmap.end())
	{
		m_log->WriteTrace("m_portmap error");
	    return false;
	}

	m_thread->del_socket(socket);

    iter1->second->close_socket(socket);	

	m_socketmap.erase(iter);
	return true;
}

bool tcp_server::add_socket(uint16 port,uint32 socket)
{
	func_lock s_lock;
	std::map<uint32,uint16>::iterator iter;
    iter = m_socketmap.find(socket);

	if(iter != m_socketmap.end())
	{
		m_log->WriteTrace("add socket is error");
	    return false;
	}

	/*char ll[1023] = {0};
	sprintf(ll,"add socket socket=%d,port=%d",socket,port);
	m_log->WriteTrace(ll);*/

	m_socketmap.insert(std::map<uint32,uint16>::value_type(socket,port));
    return true;
}

bool tcp_server::add_port(uint16 port)
{
	func_lock s_lock;
	std::map<uint16, t_socket_server*>::iterator iter;
	iter = m_portmap.find(port);

    if(iter != m_portmap.end())
		return false;

    t_socket_server*  tcp = new t_socket_server(port);
	m_thread->add_port(tcp,port);
    
	m_portmap.insert(std::map<uint16,t_socket_server*>::value_type(port,tcp));
   
	char tmp_str[1024] = {0};
	sprintf(tmp_str,"addport=%d,socket=%d,m_portmap.size=%d",
		port,tcp->get_socket(),m_portmap.size());
	m_log->WriteTrace(tmp_str);
	
	return true;
}

bool tcp_server::del_port(uint16 port)
{
	func_lock s_lock;
	std::map<uint16, t_socket_server*>::iterator iter;
	iter = m_portmap.find(port);

    if(iter != m_portmap.end())
		return false;

    std::map<uint32,uint16>::iterator iter1;
	std::map<uint32,uint16>::iterator iter2[1024];
	uint32 index = 0;
	for(iter1 = m_socketmap.begin(); iter1 != m_socketmap.end(); iter1++)
	{
		if(iter1->second == port)
		{
           iter2[index] = iter1;
		   index++;
		}
	}

	for(int i=0; i< index; i++)
	{
		del_socket(iter2[i]->first);
		m_socketmap.erase(iter2[i]);
	}

	m_thread->del_port(port);
	delete iter->second;
    m_portmap.erase(iter);
	return true;
}

